<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">织物管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick">织物信息管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex">
          <!-- <el-button v-allow="'020101'" type="primary" @click="handleAdd()">添加织物信息</el-button> -->
        </div>
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-input maxlength="50" show-word-limit placeholder="请输入织物名称或编号" v-model="searchForm.keyword" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
          <el-form-item>
            <!-- <el-button v-allow="'020102'" type="primary" @click="onSubmit">查询</el-button>
            <el-button v-allow="'020102'" @click="resetForm">重置</el-button> -->
            <el-button type="danger" plain :disabled="this.multipleSelection.length === 0" @click="handleBatchDel">停用选中</el-button>
          </el-form-item>
        </el-form>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :isSelection="isSelection" @checkbox-data="checkboxData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="operation" slot-scope="scope">
          <span @click="handleDelete(scope.row)" class="nx-btn nx-del">停用</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog"></AddEditDialog>
  </div>
</template>
<script>
import NxTable from "@/components/NxTable.vue";
import AddEditDialog from "./components/addEditDialog.vue";
import { Page } from "@/utils/pagination";
import { getCloth, delCloth } from "@/api/clothes";

export default {
  components: {
    NxTable,
    AddEditDialog,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        keyword: "",
      },
      pagesForm: {
        page: 1,
        limit: 10,
      },
      // 表格字段
      tableCols: [
        {
          label: "织物名称",
          prop: "name",
          align: "center",
        },
        {
          label: "编号",
          prop: "rfid",
          align: "center",
        },
        {
          label: "创建时间",
          prop: "create_time",
          align: "center",
        },
        {
          label: "更新时间",
          prop: "update_time",
          align: "center",
        },
        {
          label: "操作",
          slot: "operation",
          align: "center",
          width: 180,
        },
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: "", // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: "add",
      addEditId: "", // 添加编辑rowid
      isSelection: true, //复选框
      multipleSelection: [], //复选框选中集合
      ids: [], //批量停用ids
    };
  },
  // 初始化加载
  mounted() {
    // 查询
    this.getList();
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.page = 1;
      this.pagesForm.page = 1;
      this.getList();
    },
    // 分页查询
    refresh(val) {
      this.pagesForm.page = val.page;
      this.pagesForm.limit = val.limit;
      this.getList();
    },
    // 重置
    resetForm() {
      this.searchForm = {
        keyword: "",
      };
      this.pagesForm = {
        page: 1,
        limit: 10,
      };
      this.onSubmit();
    },
    // 查询列表
    getList() {
      getCloth(this.pagesForm, this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.pagination.count = res.count;
        }
      });
    },
    // 停用
    handleDelete(row) {
      this.$confirm("此操作将永久停用, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCloth([row.id]).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消停用",
          });
        });
    },
    // 批量选中数据
    checkboxData(val) {
      this.multipleSelection = val;
    },
    // 批量停用
    handleBatchDel() {
      this.ids = this.multipleSelection.map((item) => item.id);
      this.$confirm("此操作将永久停用, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCloth(this.ids).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.onSubmit();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消停用",
          });
        });
    },
    // 添加
    handleAdd() {
      this.addEditId = "";
      this.addEditStatus = "add";
      this.addEditIsDialog = true;
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + "";
      this.addEditStatus = "edit";
      this.addEditIsDialog = true;
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false;
      this.typeDialog = false;
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false;
      this.addEditIsDialog = false;
    },
  },
};
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
